package com.tian.controller.manage;

import com.tian.pojo.bo.UserManageBO;
import com.tian.pojo.vo.UserManageVO;
import com.tian.service.UserService;
import com.tian.service.manage.UserManageService;
import com.tian.utils.JSONResult;
import com.tian.utils.PagedGridResult;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("manage")
public class UserManageController {

    @Autowired
    private UserManageService userManageService;

    @Autowired
    private UserService userService;

    @GetMapping("/getUserManage")
    public JSONResult getUserManage(
            @RequestParam Integer page,
            @RequestParam Integer pageSize){
            if (page==null){
                page=1;
            }
            if (pageSize==null){
                pageSize=10;
            }

            PagedGridResult gridResult = userManageService.queryPagedUserManage(page, pageSize);

            return JSONResult.ok(gridResult);

    }

    @GetMapping("/getUserManageByUserName")
    public JSONResult getUserManageByUserName(@RequestParam String username){
        if (StringUtils.isBlank(username)){
            JSONResult.errorMsg("用户名不能为空");
        }
        List<UserManageVO> userManageByUserName = userManageService.getUserManageByUserName(username);
        return JSONResult.ok(userManageByUserName);
    }

    @PostMapping("/insertUserManage")
    public JSONResult insertUserManage(@RequestBody UserManageBO userManageBO){
        boolean b = userService.queryUsernameIsExist(userManageBO.getUsername());
        if (b){
            userManageService.updateUserManage(userManageBO);
        }else {
            userManageService.insertUserManage(userManageBO);
        }
        return JSONResult.ok();
    }

    @GetMapping("/deleteUserManage")
    public JSONResult deleteUserManage(@RequestParam String username){
        int i = userManageService.deleteUserManage(username);
        return JSONResult.ok();
    }
}
